package com.mftour.common.customLabel;

import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.mftour.common.cache.InitCache;
import com.mftour.moudles.entity.ConsolePermission;

public class CategoryPermissionsTag extends TagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1024473467112936600L;

	private String createOptions(List<ConsolePermission> cps, int level) {
		StringBuilder sb = new StringBuilder();
		for (ConsolePermission cp : cps) {
			sb.append("<option value='" + cp.getPermissionId() + "'>");
			/*for (int i = 0; i < level; i++) {
				sb.append("&nbsp;&nbsp;&nbsp;&nbsp;");
			}*/
			sb.append(cp.getPermissionName() + "</option>");
			if (cp.getPermissions() != null && cp.getPermissions().size() > 0) {
				sb.append(createOptions(cp.getPermissions(), level + 1));
			}
		}
		return sb.toString();
	}

	@Override
	public int doEndTag() throws JspException {
		StringBuilder sb = new StringBuilder();
		sb.append("<select class=\"form-control select2\" style=\"width: 100%;\" id=\"parentId\" name='parentId'>");
		sb.append("<option value='0'>系统</option>");
		sb.append(createOptions(InitCache.allCategoryList, 1));
		sb.append("</select>");
		try {
			 pageContext.getOut().write(sb.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return super.doEndTag();
	}
}
